Thread: [beginner] get file address from file pointer

  1. #1
    Registered User
    Join Date
    Mar 2007
    Posts
    16

    Question [beginner] get file address from file pointer

    hi,

    can anyone tell me how to get the full file address from a file pointer?

    I use the dev-c++ compiler and when I ask the user to enter the filename to save the data to
    and the user just enter a name like "text.dat" without c:\ (so not the full address). it's saved to my dev-c++ folder on the computer where the compiler is installed.

    But when I run the executable file on another computer and the user give the input "text.dat" the text.dat file is found in the same directory as where the executable is in, can you explain that to?

    thank you,

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,662
    Use getcwd() to find out which directory the program considers to be the current directory.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,210
    When you look for a file with a plain name, the system usually tries to look for it in the following places (and not necessarily in this order):

    • In the current directory that it is in.
    • In O/S system directories (ie. "C:\Windows")
    • In every directory listed inside the Environment variable PATH
    • Possibly a number of other places.

  4. #4
    Registered User
    Join Date
    Mar 2007
    Posts
    16
    okay,

    I have another question
    I have this

    char *filename = read_dyn_string();
    fptr = fopen(filename,"wb");

    let say the user can enter: "1234.dat" , "c:\1234.dat" when asking by the function read_dyn_string() (that just read a string and trim the newline at the end and put the string in the heap)

    and let say fopen succesfully open the file and fwrite succesfully write the data to the file, before closing the file I want to print this to the stdout printf("Data saved to %s", fulladdress);

    so how do I get the fulladdress? when the user might enter just the file name or enter the full file address?

    thank you,

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,662
    I've already told you - getcwd().

    If they type in something beginning with D:\ then it's an absolute path, and that is where the file will be.

    If not, then you basically take the result of getcwd() and strcat() the name they typed in.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  6. #6
    Lean Mean Coding Machine KONI's Avatar
    Join Date
    Mar 2007
    Location
    Luxembourg, Europe
    Posts
    444
    By printing the current working directory just before the filename:

    Code:
    char buffer[BUFSIZ];
        
    getcwd(buffer, sizeof(buffer));
    printf("%s", buffer);

  7. #7
    Registered User
    Join Date
    Mar 2007
    Posts
    16
    Quote Originally Posted by Salem
    I've already told you - getcwd().

    If they type in something beginning with D:\ then it's an absolute path, and that is where the file will be.

    If not, then you basically take the result of getcwd() and strcat() the name they typed in.
    sorry,

    cause I couldn't find the reference for the function (at http://www.cppreference.com/ and http://www.acm.uiuc.edu/webmonkeys/book/c_guide/ )

    could you give me a better reference site links ?

    ...

    and the method that you explain to me, does that means that I have to examine the beginning of the string to check if there is c:\ d:\ e:\ f:\ ? or maybe I could just check for ":\" ?

  8. #8
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,732
    KONI gave you the ref example on how to use it. At these time google should be your best example.

    here you go getcwd()

    ssharish2005

  9. #9
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,210

  10. #10
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,732
    and the method that you explain to me, does that means that I have to examine the beginning of the string to check if there is c:\ d:\ e:\ f:\ ? or maybe I could just check for ":\" ?
    if the user enters the absolute path then you will have check for the drive which the user has entered.

    Or if the user donst enter and absolute path and just entered the file name, use getcwd function to get the current working directory and append the file name to that path and then open the file.

    ssharish2005

  11. #11
    Registered User
    Join Date
    Mar 2007
    Posts
    16
    the user can enter a string like this: :abcd
    (with the ":")

    where could the file be saved? cause it's saved succesfully and I can open it with the same program, but I can't find it anywhere on my drive

  12. #12
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    ":abcd" is an invalid file name (at least for Windows XP). Your program must change the name somehow. Print the name of the file that it opened:
    Code:
    char filename[/*...*/];
    puts(filename);
    fopen(filename, "");
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  13. #13
    Registered User
    Join Date
    Mar 2007
    Posts
    16
    well, this is what I have

    Code:
    int saveListNewName(char *filename)
    {
        FILE *fptr;
        int i;
        char *fileAddress;
    
        //fprintf(stdout,"Saving records to %s\n", filename);
        printf("file name is >> %s\n",filename);
        fptr = fopen(filename,"wb");
        ....
    }
    the filename string is ":abcd"
    and this is the the output from the printf -> file name is >> :abcd
    and fopen succeed in opening the file (creating it)

    this is the rest of my function

    Code:
      
      ....
      if (fptr == NULL)
        {
            fprintf(stderr, "Could not save to %s - %s\n",
                             filename, sys_errlist[errno]);
            return 0;
    
        }
        for (i = 0; i < count; i++)
        {
            if (fwrite(cdPointers[i], sizeof(cd_t), 1, fptr) < 1)
            {
                fprintf(stderr, "Could not write to %s - %s\n",
                                    filename, sys_errlist[errno]);
                return 0;
            }
        }
        puts("File have been saved");
        fileAddress = getDir(filename);
        printf("File have been saved to %s",fileAddress);
        free(fileAddress);
        fclose(fptr);
        return 1;
    
    }
    [/code]

  14. #14
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    That's odd. You're using Dev-C++, eh?

    Try a simple program like this.
    Code:
    #include <stdio.h>
    
    int main(void) {
        const char *filename = ":file";
        FILE *fp = fopen(filename, "w");
    
        perror(filename);
    
        if(fp) fclose(fp);
    }
    If you get output like
    Code:
    :file: No error
    then your system can write to that file. If you get an error, then there's something up with your code above.

    If you do create that file and you simply can't find it anywhere, I recommend setting the date to say a year in the future, running your program, and searching your entire hard drive for files modified in the past day. Of course, that should be a last resort as it would take a long time.

    Some locations to check:
    Code:
    .  // the directory the program executable is in
    C:\Windows
    C:\Windows\Temp
    C:\Dev-Cpp
    C:\Dev-Cpp\Bin
    C:\Documents and Settings
    C:\Documents and Settings\User
    C:\Documents and Settings\User\My Documents
    C:\Documents and Settings\User\temp
    etc.

    [edit] Opening the file in binary mode may have something to do with it. Try it and see. [/edit]
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  15. #15
    Registered User
    Join Date
    Mar 2007
    Posts
    16
    I do get the output => :file: No error

    I have try to search (with indexing service disable) the entire c partition, but nothing found, I will try yours tips of setting the date a year further

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problems with file pointer in functions
    By willie in forum C Programming
    Replies: 6
    Last Post: 11-23-2008, 01:54 PM
  2. Need Help Fixing My C Program. Deals with File I/O
    By Matus in forum C Programming
    Replies: 7
    Last Post: 04-29-2008, 07:51 PM
  3. pointers
    By InvariantLoop in forum C Programming
    Replies: 13
    Last Post: 02-04-2005, 09:32 AM
  4. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM
  5. Im so lost at . .
    By hermit in forum C Programming
    Replies: 18
    Last Post: 05-15-2002, 01:26 AM